package com.sugar.follow.up.module.business.service.patientnfmzl;

import com.sugar.follow.up.module.system.api.user.AdminUserApi;
import com.sugar.follow.up.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.sugar.follow.up.module.business.controller.admin.patient.nfmzl.vo.*;
import com.sugar.follow.up.module.business.dal.dataobject.patientnfmzl.PatientNfmzlDO;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;

import com.sugar.follow.up.module.business.dal.mysql.patientnfmzl.PatientNfmzlMapper;

import static com.sugar.follow.up.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.*;

/**
 * 内分泌治疗 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class PatientNfmzlServiceImpl implements PatientNfmzlService {

    @Resource
    private PatientNfmzlMapper patientNfmzlMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createPatientNfmzl(PatientNfmzlSaveReqVO createReqVO, Long adminUserId) {
        // 获取医生
        AdminUserRespDTO user = adminUserApi.getUser(adminUserId);

        // 插入
        PatientNfmzlDO patientNfmzl = BeanUtils.toBean(createReqVO, PatientNfmzlDO.class);
        patientNfmzl.setDoctorId(adminUserId).setDoctorName(user.getNickname());
        patientNfmzlMapper.insert(patientNfmzl);

        // 返回
        return patientNfmzl.getId();
    }

    @Override
    public void updatePatientNfmzl(PatientNfmzlSaveReqVO updateReqVO) {
        // 校验存在
        validatePatientNfmzlExists(updateReqVO.getId());
        // 更新
        PatientNfmzlDO updateObj = BeanUtils.toBean(updateReqVO, PatientNfmzlDO.class);
        patientNfmzlMapper.updateById(updateObj);
    }

    @Override
    public void deletePatientNfmzl(Long id) {
        // 校验存在
        validatePatientNfmzlExists(id);
        // 删除
        patientNfmzlMapper.deleteById(id);
    }

    @Override
        public void deletePatientNfmzlListByIds(List<Long> ids) {
        // 删除
        patientNfmzlMapper.deleteByIds(ids);
        }


    private void validatePatientNfmzlExists(Long id) {
        if (patientNfmzlMapper.selectById(id) == null) {
            throw exception(PATIENT_NFMZL_NOT_EXISTS);
        }
    }

    @Override
    public PatientNfmzlDO getPatientNfmzl(Long id) {
        return patientNfmzlMapper.selectById(id);
    }

    @Override
    public PageResult<PatientNfmzlDO> getPatientNfmzlPage(PatientNfmzlPageReqVO pageReqVO) {
        return patientNfmzlMapper.selectPage(pageReqVO);
    }

}